package com.uenpay.im.invokeitems;

import com.qiyunxin.android.http.net.HttpInvokeItem;
import com.qyx.android.entity.GroupMemberEntity;
import com.qyx.android.entity.GroupTalkEntity;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.serializations.GroupMemberSerializer;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * 群详情
 * 
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class GetGroupMessageDetail extends HttpInvokeItem {

	public class GetGroupMessageDetailResult {
		public int status;
		public String msg;

		public GroupTalkEntity groupTalk = new GroupTalkEntity();
		public ArrayList<GroupMemberEntity> participants;
		public ArrayList<Long> deleteCustIdArr = new ArrayList<Long>();
	}

	public GetGroupMessageDetailResult getOutput() {
		return (GetGroupMessageDetailResult) GetResultObject();
	}

	public GetGroupMessageDetail(String group_id) {
		String url = APIConfiguration.getBaseUrl()
				+ "Chat/Chat/getChatDetail?chatid=" + group_id + "&"
				+ APIConfiguration.getCustIdAndToken();
		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) {
		GetGroupMessageDetailResult result = new GetGroupMessageDetailResult();
		try {

			JSONObject json = new JSONObject(responseBody);
			result.status = json.optInt("status");
			result.msg = json.optString("msg");
			JSONObject dataJson = json.optJSONObject("data");

			if (dataJson != null) {
				result.groupTalk.group_info_json = dataJson.toString();
				result.groupTalk.introduce = dataJson.optString("introduce");
				result.groupTalk.creator_cust_name = dataJson
						.optString("nickname");
				if (dataJson.has("_nickname")) {
					result.groupTalk.my_name_in_group = dataJson
							.optString("_nickname");
				}
				result.groupTalk.group_name = dataJson.optString("chatname");
				result.groupTalk.group_id = Long.valueOf(dataJson
						.optString("chatid"));
				result.groupTalk.max_count = dataJson.optInt("maxcustnum");
				result.groupTalk.has_count = dataJson.optInt("custnum");
				result.groupTalk.is_admin = dataJson.optInt("_isadmin");
				result.groupTalk.join_status = dataJson.optInt("_exist");
				result.groupTalk.creator_cust_id = dataJson.optString("custid");
				result.groupTalk.org_type_id = dataJson.optInt("chattype");
				if (dataJson.has("custList")) {
					JSONArray joArray = dataJson.optJSONArray("custList");
					if (joArray != null) {
						result.participants = GroupMemberSerializer
								.deserializeUsers(joArray);
						dataJson.remove("custList");
					}
				}

			}
		} catch (JSONException e) {
			e.printStackTrace();
			return result;
		}

		return result;
	}

}
